package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.web.mvc.controller.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * 输出 “Hello,World” Controller
 */
@Path("/user")
public class RegisterController implements RestController {
    @Resource(name = "bean/UserService")
    private UserService userService;

    @GET
    @POST
    @Path("/register")
    public String register(HttpServletRequest request, HttpServletResponse response,@Valid User user) throws Throwable {

        Map<String,String> error =(HashMap<String,String>) request.getAttribute("error");
        if(error != null && error.size() > 0) {
            return "register-form.jsp";
        }else{
            boolean register = userService.register(user);
            if (register) {
                return "login-form.jsp";
            } else {
                request.setAttribute("message","用户密码有人注册了，请换一个！");
                return "register-form.jsp";
            }
        }
    }
}
